const fetch = require('../../utils/fetch')
import Toast from '@vant/weapp/toast/toast';
import Tool from "../../utils/tool";
import Cache from "../../utils/cache";
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    keywrod: '',
    baseurl: app.config.apiBase,
    baseimgurl: app.config.apiImgBase, 
    searchPageNum: 1, // 设置加载的第几次，默认是第一次
    callbackcount: 8, //返回数据的个数
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏
    hasmore: false, //“没有数据”的变量，默认false，隐藏  
    isShowsSearchIput: false, // 搜索框 
    groupons: [], 
    banner: [{
        url: '/static/images/top1.jpg'
      }
      // ,
      // {
      //   url: '/static/images/top2.jpg'
      // }
    ],
    channel: [ 
    ],
    coupon: [],
    notice: '阳哥杂谈每日分享最新资讯，素材来源于网络，如有侵权，请联系微信lhsj202206      ',
    goodsCount: 0,
    indicatorDots: false,
    window: false,
    colseCoupon: false
  },
  showInputSearch: function () {
    this.setData({
      isShowsSearchIput: true
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { 
          wx.setNavigationBarTitle({
            title: "阳哥杂谈" //页面标题为路由参数
          })
    this.setData({
      searchPageNum: 1,
      hasmore: true
    });
        //获取分类信息
   this.getCategoryInfo();
    this.getmore();

 
  },
   //获取分类信息
  getCategoryInfo:function ( ) {
    //先读缓存  缓存没有再读  
  let that = this;
  fetch('/classList',{},'GET',{},false)
  .then(res => {
    console.log(res.data);
    //成功
    if (res.data.code === 200&&res.data.data) {
      //返回不为空，则表示有数据  
    
      if(res.data.data.length>9){
        var innerList=[] 
       for( i=0;i<res.data.data.length;i++){
        innerList.concat(res.data.data[i])
       }
       that.setData({
        channel: innerList
      }); 
      }else{
        that.setData({
          channel: res.data.data
        }); 
      }
    
      
    } 
  }).catch(res => {
    //提示异常，稍后重试

  }) 
 
  }
  , 
 
  getmore: function () {

    let that = this;
    that.setData({
      searchLoading: true //显示加载中
    });
    //    fetch(`/shops/${options.item}`)
    fetch('/list?pageNum=' + that.data.searchPageNum + '&pageSize=' + that.data.callbackcount)
      .then(res => {
        console.log(res.data);
        //成功
        if (res.data.code === 200) {
          //返回不为空，则表示有数据
          if (res.data.rows) {
            //判断返回的数据是否小于 页数请求，如果小于则表示已经加载完
            if (res.data.rows.length < that.data.callbackcount) {
              that.data.hasmore = false;
            } else if ( (that.data.callbackcount* that.data.searchPageNum)>= res.data.total ) {
              that.data.hasmore = false;
            } else {
              //还有更多
              that.data.hasmore = true;
            }
            //页数请求增加1
            that.data.searchPageNum = that.data.searchPageNum + 1
            that.data.groupons = that.data.groupons.concat(res.data.rows)
          } else {
            //返回成功  但是没有课程  则
            hasmore = false;
          }
          that.setData({
            groupons: that.data.groupons, //获取数据数组 
            hasmore: that.data.hasmore,
            searchPageNum: that.data.searchPageNum,
            searchLoading: false //把"上拉加载"的变量设为false，显示
          });
        }
      }).catch(res => {
        //提示异常，稍后重试

      })

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.hasmore) {
      this.getmore();
    }
  },
  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value
    });


  },
  inputFocus: function () {

  },
  onShareAppMessage: function() {
		wx.showShareMenu({
	  withShareTicket: true,
	  menus: ['shareAppMessage', 'shareTimeline']
	 })
	},
	//用户点击右上角分享朋友圈
	onShareTimeline: function () {
		return {
	  title: '阳哥杂谈-掌握第一资讯',
	  query: {
	  key: value
	  } 
	 }
	},
  onKeywordConfirm(event) {
    //搜索事件吧
    console.log(event)
    if (Tool.isEmpty(event.detail.value)) {
      Toast.success('请输入内容');
    } else{
      //跳转至搜索结果界面 
      wx.navigateTo({
        url: '/pages/catitem/catitem?query='+event.detail.value
      })
    }


  },
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //请求最新更新
    this.setData({
      searchPageNum: 1,
      groupons: [],
      hasmore: true
    });
    this.getmore(); 
  },
  clearKeyword: function () {
    this.setData({
      keyword: ''
    });
  },
  //隐藏搜索录入
  closeSearch: function () {
    this.setData({
      isShowsSearchIput: false
    });
  }
})